home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5316 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  95 lines

  1. Path: magnus.acs.ohio-state.edu!csn!ub!newserve!rebecca!rpi!not-for-mail
  2. From: tonyh@tcp.co.uk
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
  4. Subject: Re: Q: Rigorous coding using #if !defined(...) and #include
  5. Date: 2 Feb 1996 23:58:03 -0000
  6. Organization: UWL
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: herbs@connobj.com
  9. Message-ID: <4eu8eb$npt@netlab.cs.rpi.edu>
  10. References: <4eo1fs$rr3@netlab.cs.rpi.edu>
  11. Reply-To: tonyh@tcp.co.uk
  12. NNTP-Posting-Host: netlab.cs.rpi.edu
  13. X-Original-Date: Fri, 02 Feb 1996 20:50:03 GMT
  14.  
  15. On 31 Jan 1996 15:22:36 -0000 David Carr wrote:
  16.  
  17. > Okay, nuff of the preamble.  The problem is this.  I have 
  18. > another class;
  19. > #if !defined (__OTHER_H)
  20. > #define (__OTHER_H)
  21. > #include "MyClass.h"
  22. > class Other
  23. > {
  24. ...// Uses something in MyClass.h
  25. > };
  26. > #endif
  27. > and in my source for MyClass.cpp, MyClass.h is obviously 
  28. > included.  Now,
  29. > when I try to compile MyClass.cpp, MyClass.h is included and 
  30. > __MYCLASS_H 
  31. > becomes defined.  Then, MyClass.h #includes Other.h, thus 
  32. > defining __OTHER_H.
  33. > Now, Other.h #includes MyClass.h.  As __MYCLASS_H has already 
  34. > been defined,
  35. > the code in MyClass.h is not included.  Returning to Other.h, 
  36. > this code
  37. > references something in MyClass.h (i.e. its class) and a 
  38. > compiler error is
  39. > generated because the symbol/identifier/whatever is unknown.
  40.  
  41. It sounds like either you haven't split up your declarations/definitions
  42. into enough header files or you've got some circular dependencies that
  43. wouldn't work anyway.
  44.  
  45. If you have something like:
  46.  
  47. class A {
  48.   B *p;
  49. public:
  50.   do_something();
  51. };
  52.  
  53. class B {
  54. public:
  55.   void use_a(A *p)
  56.   {
  57.     a->do_something();
  58.   }
  59. };
  60.  
  61. class B needs a definition of class A because it accesses one or more
  62. members. But class A only has a pointer to B, so it only needs to know
  63. that there is some class called B, without knowing its definition ie
  64. change the member to:
  65.  
  66. class B *p;
  67.  
  68. or declare:
  69.  
  70. class B;
  71.  
  72. before A's definition.
  73.  
  74. You can't have two classes that use each other's members unless you do
  75. it through an abstract interface class.
  76.  
  77. -- 
  78. -------------------------------------------------------------------
  79. Tony Houghton                           tonyh@tcp.co.uk 
  80. Using a RiscPC 700                      http://www.tcp.co.uk/~tonyh
  81. -------------------------------------------------------------------
  82. You don't call cancer a 'popular' disease,
  83. so why call Windows a popular operating system?
  84.  
  85.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  86.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  87.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  88.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  89.